import time
import requests
import json


# 1718957821074
# 1718958056204
# 1718958038644.8542
# 1718958016.4929652
# print(int(time.time()*1000))

def method1():
    for i in range(1, 282):
        pn = i
        now = int(time.time() * 1000)
        url = f"https://72.push2.eastmoney.com/api/qt/clist/get?cb=jQuery112402785954860355875_1718957821073&pn={pn}&pz=20&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&dect=1&wbp2u=|0|0|0|web&fid=f3&fs=m:0+t:6,m:0+t:80,m:1+t:2,m:1+t:23,m:0+t:81+s:2048&fields=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f22,f11,f62,f128,f136,f115,f152&_={now}"
        response = requests.get(url)
        # content text json()
        response = response.text[42:-2]
        datas = json.loads(response)['data']['diff']
        for data in datas:
            print(data['f14'], data['f3'], data['f17'])

        time.sleep(1)


def method2():
    for i in range(1, 282):
        pn = i
        now = int(time.time() * 1000)
        url = f"https://72.push2.eastmoney.com/api/qt/clist/get?pn={pn}&pz=20&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&dect=1&wbp2u=|0|0|0|web&fid=f3&fs=m:0+t:6,m:0+t:80,m:1+t:2,m:1+t:23,m:0+t:81+s:2048&fields=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f22,f11,f62,f128,f136,f115,f152&_={now}"
        response = requests.get(url)
        datas = response.json()['data']['diff']
        for data in datas:
            print(data['f14'], data['f3'], data['f17'])
        time.sleep(1)

        break


# method2()


import csv


class RequestEast:
    def __init__(self):
        self.f = open("east.csv", "w", newline="", encoding="utf8")
        self.csv_writer = csv.writer(self.f)
        self.csv_writer.writerow(["股票名称", "最高价", "最低价"])

    def start_request(self):
        for i in range(1, 5):
            pn = i
            now = int(time.time() * 1000)
            url = f"https://72.push2.eastmoney.com/api/qt/clist/get?pn={pn}&pz=20&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&dect=1&wbp2u=|0|0|0|web&fid=f3&fs=m:0+t:6,m:0+t:80,m:1+t:2,m:1+t:23,m:0+t:81+s:2048&fields=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f22,f11,f62,f128,f136,f115,f152&_={now}"
            response = requests.get(url)
            self.__parse(response)
            time.sleep(1)

    def __parse(self, response):
        datas = response.json()['data']['diff']
        for data in datas:
            self.__save(data)

    def __save(self, data):
        self.csv_writer.writerow([data['f14'], data['f3'], data['f17']])

    def __del__(self):
        self.f.close()


re = RequestEast()
re.start_request()
